package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Role;
import com.qk.management.mapper.RoleMapper;
import com.qk.management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;


    @Override
    public void deleteById(Integer id) {
        roleMapper.deleteById(id);
    }

    @Override
    public void add(Role role) {
        boolean b = BeanUtil.hasNullField(role, "id", "remark", "updateTime", "createTime");
        if (b) {
            throw new RuntimeException("参数错误");
        }
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insert(role);
    }

    @Override
    public Role getbyId(Integer id) {
        return roleMapper.getbyId(id);
    }

    @Override
    public void update(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.update(role);
    }





    @Override
    public List<Role> selectList() {
        return roleMapper.selectAll();
    }

    @Override
    public PageResult<Role> page(String name, String label, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Role> roleList = roleMapper.selectByPage(name, label);
        Page<Role> rolePage = (Page<Role>) roleList;
        return PageResult.<Role>builder()
                .total((int) rolePage.getTotal())
                .rows(rolePage.getResult())
                .build();
    }
}
